﻿/*
*┌────────────────────────────────────────────────┐
*│　描    述：NullConvert                                                   
*│　作    者： (Jxw)                                             
*│　版    本：1.0                                              
*│　创建时间：2020/8/31 14:24:46                        
*└────────────────────────────────────────────────┘
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Utility
{

    /// <summary>
    /// 空值处理类
    /// </summary>
    public class NullConvert
    {
        /// <summary>
        /// 将可为空的整型转化为0
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static int IntNullDispose(Nullable<int> arg)
        {
            return arg.HasValue ? arg.Value : 0;
        }

        /// <summary>
        /// 将可为空的整型转化为0
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static int IntNullDispose(string arg)
        {
            return string.IsNullOrWhiteSpace(arg) ? 0 : Convert.ToInt32(arg);
        }

        /// <summary>
        /// 将可为空的浮点转化为0
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static double DoubleNullDispose(string arg)
        {
            return string.IsNullOrWhiteSpace(arg) ? 0 : Convert.ToDouble(arg);
        }


        /// <summary>
        /// 将Null字符串转换为""
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static string StringNullToEmpty(string arg)
        {
            return (arg != null) ? arg : "";
        }
    }


    /// <summary>
    /// 模型转换类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ModelConvertHelper<T> where T : new()
    {
        /// <summary>
        /// DataTable类型转换成集合实体
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List<T> ConvertToModel(DataTable dt)
        {
            // 定义集合    
            List<T> ts = new List<T>();

            // 获得此模型的类型   
            Type type = typeof(T);
            string tempName = "";

            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();
                // 获得此模型的公共属性      
                PropertyInfo[] propertys = t.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;  // 检查DataTable是否包含此列    

                    if (dt.Columns.Contains(tempName))
                    {
                        // 判断此属性是否有Setter      
                        if (!pi.CanWrite) continue;

                        object value = dr[tempName];
                        if (value != DBNull.Value)
                            pi.SetValue(t, value, null);
                    }
                }
                ts.Add(t);
            }
            return ts;
        }
    }

}
